package com.itheima.controller;

import com.itheima.advice.LogOperation;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.StuQueryParam;
import com.itheima.pojo.Student;
import com.itheima.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StuController {
    @Autowired
    StuService stuService;
    @GetMapping
    Result listByPage( StuQueryParam stuQueryParam){
        PageResult studentList =stuService.listByPage(stuQueryParam);
        return Result.success(studentList);
    }
    @PostMapping
    @LogOperation
    Result saveStu(@RequestBody Student student){
        stuService.saveStu(student);
        return Result.success();
    }
    @GetMapping("/{id}")
    Result findById(@PathVariable Integer id){
        Student student = stuService.findById(id);
        return Result.success(student);
    }
    @PutMapping
    @LogOperation
    Result updateStu(@RequestBody Student student){
        stuService.updateStu(student);
        return Result.success();
    }
    @DeleteMapping("{ids}")
    @LogOperation
    Result delByIds(@PathVariable List<Integer> ids){
        stuService.delByIds(ids);
        return Result.success();
    }
    @PutMapping("/violation/{id}/{score}")
    @LogOperation
    Result updateViolation(@PathVariable Integer id , @PathVariable Integer score){
        stuService.updateViolation(id,score);
        return Result.success();
    }


}
